package org.scorpios.search.jieba;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MtyStringUtils {
    /**
     * 该方法和 {@link Pattern#split(CharSequence)} 功能一样,利用pattern匹配规则分割字符str, 但是会把匹配的分割符串也返回.
     * 比如字符串 "hello123word" 用 (\d+) 分割, 会返回["hello", "123", "word"]
     *
     * @param pattern
     * @param str
     * @return
     */
    public static List<String>  splitAndReturnDelimiters(Pattern pattern, String str) {
        Matcher matcher = pattern.matcher(str);
        List<String> result = new ArrayList<String>();
        int strLen = str.length();
        int lastMatchIdx = 0;
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            String ds = matcher.group();

            if (lastMatchIdx != start) {
                String leftS = str.substring(lastMatchIdx, start);
                result.add(leftS);
            }

            result.add(ds);

            lastMatchIdx = end;
        }

        if (lastMatchIdx < strLen) {
            result.add(str.substring(lastMatchIdx, strLen));
        }
        return result;

    }

    /**
     * 获取字符串拼音的第一个字母
     */
    public static String toFirstChar(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    /**
     * 汉字转为拼音
     */
    public static String toPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    public static String filterCharacter(String content){
        content = content.replaceAll("[(ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹ)]","");
        return content;
    }


}

